<template>
    <view class="category_container flex">
        <view class="category_sidebar">
            <view class="sidebar_item" :class="typeIndex === index ? 'active' : ''" v-for="(item, index) in CategoryTree"
                :key="item.id" @click="clickHandler(index, item.id)">{{ item.name }}</view>
        </view>
        <navigator
            url="/pages/"
            open-type="navigate"
            hover-class="navigator-hover"
        >
        <view class="category_content">
            <view class="content_title">{{ categoryInfo?.name }}</view>
            <view class="content_list flex">
                <view class="list_item flex_c" v-for="item in categoryInfo.children" :key="item.id">
                    <image :src="item.imageUrl" mode="scaleToFill" />
                    <text>{{ item.name }}</text>
                </view>
            </view>
        </view>
    </navigator>
    </view>
</template>

<script lang="ts">
export default {
    name: "classify"
}
</script>
<script lang="ts" setup>
import { findCategoryTree } from '@/Api/category'
import { onLoad, onShow } from '@dcloudio/uni-app';
import { ref, watch } from 'vue'
onLoad(() => {
    findCategoryTreehandler()
})
interface categoryList {
    [propsName: string]: any
}
// 定义响应式数据
const CategoryTree = ref<categoryList>([])
const typeIndex = ref(0)
const categoryInfo = ref()

// 定义功能函数
const findCategoryTreehandler = async () => {
    try {
        let result = await findCategoryTree()
        if (result.code === 200) {
            CategoryTree.value = result.data
            categoryInfo.value = CategoryTree.value[0]
        }
    } catch (error) {
        console.log(error);

    }
}
const clickHandler = (index: number, id: number) => {
    typeIndex.value = index

}
watch(() => typeIndex.value, (newIndex) => {
    categoryInfo.value = CategoryTree.value[newIndex]
})
</script>

<style lang="less">
.flex {
    display: flex;
}

.flex_c {
    display: flex;
    flex-direction: column;
}

.category_container {
    width: 100%;
    height: 100%;

    .category_sidebar {
        width: 80px;
        height: 100%;
        background-color: #eee;

        .sidebar_item {
            height: 36px;
            width: 100%;
            line-height: 36px;
            text-align: center;
            font-size: 13px;
        }

        .active {
            background-color: #fff;
        }

        .active::before {
            content: "";
            position: absolute;
            left: 2px;
            width: 2px;
            height: 34px;
            background-color: red;
        }

    }

    .category_content {
        width: 296px;
        padding: 10px;

        .content_list {
            .list_item {
                width: 33.33%;
                justify-content: center;
                align-items: center;

                image {
                    width: 30px;
                    height: 30px;
                }

                text {
                    font-size: 12px;
                }
            }
        }
    }

}
</style>